/*
 * ExceptionDialog.java
 *
 * Created on 3 de Setembro de 2006, 12:33
 */

package info.carl.jgdt.gui;

import java.awt.Frame;

/**
 *
 * @author  carlson
 */
public class ExceptionDialog extends javax.swing.JDialog {
    
    /** Creates new form ExceptionDialog */
    public ExceptionDialog(Frame parent, Exception e){
        super(parent, true);
        initComponents();
        lbErrorMessage.setText(e.getLocalizedMessage());
        String exceptionString = e.getLocalizedMessage().concat(" caused by: \n");
        for (StackTraceElement ste: e.getStackTrace()){
            exceptionString = exceptionString.concat(ste.toString());
            exceptionString = exceptionString.concat("\n");
        }
        taDetails.setText(exceptionString);
        taDetails.setCaretPosition(0);
        // To center this dialog screen relative to the main screen
        int x = ((int)(parent.getWidth() - getWidth()) / 2) + parent.getLocation().x;
        int y = ((int)(parent.getHeight() - getHeight()) / 2) + parent.getLocation().y;
        this.setLocation(x, y);
        setVisible(true);
    }
    
    public ExceptionDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        tbErrorMessage = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        lbErrorMessage = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taDetails = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);
        setResizable(false);
        lbErrorMessage.setText("errorMessage");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(lbErrorMessage)
                .addContainerGap(383, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(lbErrorMessage)
                .addContainerGap(168, Short.MAX_VALUE))
        );
        tbErrorMessage.addTab("Error Message", jPanel1);

        jPanel2.setLayout(new java.awt.BorderLayout());

        taDetails.setColumns(20);
        taDetails.setRows(5);
        jScrollPane1.setViewportView(taDetails);

        jPanel2.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        tbErrorMessage.addTab("Details", jPanel2);

        jButton1.setText("Close");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Details");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(319, Short.MAX_VALUE)
                .add(jButton2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton1)
                .addContainerGap())
            .add(tbErrorMessage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 484, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(tbErrorMessage, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 222, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton2)
                    .add(jButton1))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        tbErrorMessage.setSelectedIndex(1);
    }//GEN-LAST:event_jButton2ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ExceptionDialog(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbErrorMessage;
    private javax.swing.JTextArea taDetails;
    private javax.swing.JTabbedPane tbErrorMessage;
    // End of variables declaration//GEN-END:variables
    
}
